<?php  
    namespace Home\Controller;
    use Think\Controller;
   
    class ShiftController extends AllowController{
       
       	//显示stu模块
    	public function index(){
    		// 实例化
    		$mod = M('shift');
            $users = M('users');

    		// session 获取学生自己的学生id 
    		$id=$_SESSION["userid"]; 
            //总条数
            $tot=$mod ->where("name_id={$id}")->Count();

            $nums = 10;
            $page = new \Think\Page($tot,$nums);
            //根据id查询users下学生的名字 
            $name= $users->where("id=$id")->field('name')->find();
            
            //根据学生自己的id查询自己的申请的信息
    		$list = $mod->where("name_id={$id}")->order("sendtime desc")->limit($page->firstRow,$page->listRows)->select();
    		
            //实例化
    		$mods = M('class');


    		// 遍历 把时间戳转换日期
    		foreach($list as $key=>$value){
                $list[$key]['name_id']=$name['name'];
                $list[$key]['sendtime'] = date("Y-m-d H:i:s",$value['sendtime']);

                //传值给查看desc信息唯一值
    			$list[$key]['descid'] = $value['sendtime'];
    			
                //查询班级的名字
    			$classlist = $mods->where("id={$list[$key]['newclass']}")->field("name")->select();

                //所在班级显示名字
                $oldcs =  $mods->where("id={$list[$key]['oldclass']}")->field("name")->find();

                //把学生id转换名字显示
                $list[$key]["oldcs"] = $oldcs['name'];

                //将班级的数值转换名字
	    		switch ($value["type"]) {
	    			case 1:
	    				$list[$key]["typename"]="转班 （转到 {$classlist["0"]["name"]} 班）";
	    				break;
	    			case 2:
	    				$list[$key]["typename"]="转校区";
	    				break;
	    			case 3:
	    				$list[$key]["typename"]="见习";
	    				break;
	    			case 4:
	    				$list[$key]["typename"]="休学";
	    				break;
	    			case 5:
	    				$list[$key]["typename"]="退学";
	    				break;
	    		}

                 //将状态数值转换名字
	    		switch ($value["status"]) {
	    			case 0:
	    				$list[$key]["color"]="warm";
	    				$list[$key]["statusname"]="待审核";
	    				break;
	    			case 1:
                        $list[$key]["color"]="success";
                        $list[$key]["statusname"]="通过";	    				
	    				break;
	    			case 2:
	    				$list[$key]["color"]="danger";
                        $list[$key]["statusname"]="拒绝";
	    				break;
	    		}

    		}
    		// 分配值和导入模版
            if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
            //分配数据
            $this->assign('list',$list);
            $this->assign('curr',$p);
            $this->assign('page',ceil($tot/$nums));
            $this->assign('total',$tot);

    		$this->display("Shift/stu");

    	}

    	// stu显示new模块
    	public function add(){
    		// 实例化 
    		$mod = M("class");
    		$list = $mod->select();
            $names = $_SESSION['name'];
    		//session传值 把自己的班级id传到默认隐藏区域 把班级禁用
    		$nowclass = $_SESSION["class"];

    		// 把班级信息显示到new页面
            $this->assign("list",$list);
    		$this->assign("names",$names);

    		$this->assign("nowclass",$nowclass);
    		$this->display("Shift/add");
    	}

    	// stu新建插入数据
    	public function insert(){
    		$mod = M('shift');
    	 	// 两个地方需要session数据
    	 	//学生姓名
    	 	$data["name_id"]=$_SESSION["userid"];
    	 	// $data["name_id"]= 110;

    	 	//原班级用session
    	 	$data['oldclass'] = $_SESSION["class"];
    	 	// $data["oldclass"] = 6;

    	 	$data["type"]=$_POST["type"];

    	 	// type=1转班 要把转班的班级id获取到
    	 	if ($data["type"] == 1) {
    	 		$data["newclass"] = $_POST["newclass"];
    	 	}else{
    	 		$data["newclass"] = $data["oldclass"];
    	 	}
    	 	$data["desc"]=$_POST["desc"];
    	 	$data["sendtime"]=time();
    	 	$data["status"]=0;
    	 	
			if ($mod->add($data)) {
				echo "1";
			}else{
				echo "0";
			}
    	}

    	// teacher模块
        public function indexs(){
            // 实例化
            $mod = M('shift');
            $users = M('users');
            $mods = M('class');

            //分页
            $tot=$mod->Count();

            $nums = 3;
            $page = new \Think\Page($tot,$nums);
            //根据时间戳排序
            $list = $mod->order("sendtime desc")->limit($page->firstRow,$page->listRows)->select();

            // 遍历 把时间戳转换日期
            foreach($list as $key=>$value){

                //根据id查询users表下的学生名字
                $name= $users->where("id={$list[$key]['name_id']}")->field('name')->find();

                //新创建一个name下标把名字赋值
                $list[$key]['name'] = $name['name'];

                //根据id查询class表下的班级名字
                $oldcs =  $mods->where("id={$list[$key]['oldclass']}")->field("name")->find();

                //新创建一个oldcs的下标，班级名字赋值
                $list[$key]["oldcs"] = $oldcs['name'];

                //转换时间戳
                $list[$key]['sendtime'] = date("Y-m-d H:i:s",$value['sendtime']);

                //传值给查看desc信息唯一值
                $list[$key]['descid'] = $value['sendtime'];
                
                $classlist = $mods->where("id={$list[$key]['newclass']}")->field("name")->select();

                //将班级的数值转换名字
                switch ($value["type"]) {
                    case 1:
                        $list[$key]["typename"]="转班 （转到 {$classlist["0"]["name"]} 班）";
                        break;
                    case 2:
                        $list[$key]["typename"]="转校区";
                        break;
                    case 3:
                        $list[$key]["typename"]="见习";
                        break;
                    case 4:
                        $list[$key]["typename"]="休学";
                        break;
                    case 5:
                        $list[$key]["typename"]="退学";
                        break;
                }

                //将状态数值转换名字
                switch ($value["status"]) {
                    case 0:
                        $list[$key]["color"]="warm";
                        $list[$key]["statusname"]="待审核";
                        break;
                    case 1:
                        $list[$key]["color"]="success";
                        $list[$key]["statusname"]="通过";                     
                        break;
                    case 2:
                        $list[$key]["color"]="danger";
                        $list[$key]["statusname"]="拒绝";
                        break;
                }

            }
            // 分配值和导入模版
            if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
            //分配数据
            $this->assign('list',$list);
            $this->assign('curr',$p);
            $this->assign('page',ceil($tot/$nums));
            $this->assign('total',$tot);
            $this->display("Shift/teacher");
        }



        // teacher显示news模块
        public function news(){
            // 实例化 
            $mod = M("class");
            $list=$mod->select();

            // 把班级信息显示到new页面
            $this->assign("list",$list);
            $this->display("Shift/news");
        }

        //查询班级
        public function refer(){
            //接收
            $classid=I("post.classid");
            //实例化
            $mod = M("users");

            $list=$mod->where("class=$classid")->field("id,name")->select();

            //返回json
            echo json_encode($list);

        }

        // teacher新建插入数据
        public function inserts(){
            //实例化
            $mod = M('shift');
            $mods = M("users");
            //学生姓名
            $id=I("post.nameid");
            $data["name_id"]= $id;
            $oldclass = $mods->where("id=$id")->field("class")->find();
            //原班级
            $data["oldclass"] = $oldclass['class'];

            $data["type"]=$_POST["type"];

            // type=1转班 要把转班的班级id获取到
            if ($data["type"] == 1) {
                $data["newclass"] = $_POST["newclass"];
            }else{
                $data["newclass"] = $data["oldclass"];
            }
            $data["desc"]=$_POST["desc"];
            $data["sendtime"]=time();
            $data["status"]=1;
            if ($mod->add($data)) {
                $nclass['class'] = $data["newclass"];
				$nclass['team'] = 0;
				$nclass['role_id'] = 10;
                $mods->where("id=$id")->save($nclass);
                echo "1";
            }else{
                echo "0";
            }
        }

        //修改状态 通过
        public function update(){
            //实例化
            $mod = M('shift');
            $mods = M("users");
            $da['id'] = I('post.id');

            //只能在状态等于0才能修改
           if(!$_POST['status']==0){
                echo "0";
                exit;
           }
           //根据id查询学生的信息
            $lists = $mod->where($da)->find();
            $date['class']= $lists['newclass'];

            $date['team']= 0;
			$date['role_id']= 10;
            $nameid= $lists['name_id'];

            //改变学生班级和team值
            $mods->where("id=$nameid")->save($date);
            $_SESSION["class"] = $lists['newclass'];
            //默认设置状态值
            $data['status'] = 1;      
            $list=$mod->where($da)->save($data);
            
            if($list){
                echo "1";
            }else{
                echo "0";
            }
        }

        // 拒绝
        public function updates(){
            $mod = M('shift');
            $da['id'] = I('post.id');
            
             //只能在状态等于0才能修改
            if($_POST['status']==1||$_POST['status']==2){
                echo "0";
                exit;
            }
            $data['status'] = 2;
            $list=$mod->where($da)->save($data);
            // echo $list;exit;
            if($list){
                echo "1";
            }else{
                echo "0";
            }
        }

    }
?>